package com.sys.midware.config.factory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.sys.midware.config.core.Configuration;
import com.sys.midware.config.model.BNode;

/**
 * 对XML文件进行配置解析的工厂
 * 
 */
public class XMLConfigurationFactory implements AbstractConfigurationFactory {


    @Override
    public void parseConfigFile(InputStream input, Configuration cfg) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(input);
        Element rootEle = doc.getDocumentElement();
        // 加载配置文件
        NodeList configfile = rootEle.getElementsByTagName("import");
        if(configfile!=null && configfile.getLength()>0){
            Map<String, String> fileMap = new HashMap<String, String>();
            int configfileLength = configfile.getLength();
            for (int i = 0; i < configfileLength; i++) {
                Element node = (Element) configfile.item(i);
                String name = node.getAttribute("name");
                String value = node.getAttribute("value");
                fileMap.put(name, value);
            }
            cfg.setConfigFiles(fileMap);
        }
       
        //普通的配置文件
        NodeList propList = rootEle.getElementsByTagName("property");
        if(propList!=null && propList.getLength() >0){
            for (int i = 0; i < propList.getLength(); i++) {
                Element node = (Element) propList.item(i);
                String name = node.getAttribute("name");
                String value = node.getAttribute("value");
                cfg.addParam(name, value);
            }
        }
 
        //集群node
        NodeList nodeList = rootEle.getElementsByTagName("node");
        if(nodeList !=null && nodeList.getLength()>0){
            List<BNode> bnodeList = new ArrayList<BNode>();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Element nodeEle = (Element) nodeList.item(i);
                BNode bnode = new BNode();
                String name = nodeEle.getAttribute("name");
                String nodeType = nodeEle.getAttribute("type");
                String nodeFor = nodeEle.getAttribute("for");
                String nodeDefault = nodeEle.getAttribute("isDefault");
                bnode.setNodeName(name);
                bnode.setNodeType(nodeType);
                bnode.setNodeFor(nodeFor);
                bnode.setNodeDefault(nodeDefault);
                NodeList childPropList = nodeEle.getElementsByTagName("node-property");
                Map<String, Object> childPropMap = new HashMap<String, Object>();
                for (int c=0; c < childPropList.getLength() ;c++ ){
                    Element childProp = (Element) childPropList.item(c);
                    String childPName = childProp.getAttribute("name");
                    String childPValue = childProp.getAttribute("value");
                    childPropMap.put(childPName, childPValue);
                }
                bnode.setNodeParams(childPropMap);
                bnodeList.add(bnode);
                cfg.addParam(name, bnode);
                cfg.setBNodeList(bnodeList);
            }
        }

    }

}
